package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMemberRaiMapper;
import com.ruoyi.system.domain.SysMemberRai;
import com.ruoyi.system.service.ISysMemberRaiService;

/**
 * 会员权益Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysMemberRaiServiceImpl implements ISysMemberRaiService 
{
    @Autowired
    private SysMemberRaiMapper sysMemberRaiMapper;

    /**
     * 查询会员权益
     * 
     * @param id 会员权益主键
     * @return 会员权益
     */
    @Override
    public SysMemberRai selectSysMemberRaiById(String id)
    {
        SysMemberRai sysMemberRai = sysMemberRaiMapper.selectSysMemberRaiById(id);
        List<String> raiGoods = sysMemberRaiMapper.selectRaiGoods(id);
        sysMemberRai.setRaiGoods(raiGoods);
        return sysMemberRai;
    }

    /**
     * 查询会员权益列表
     * 
     * @param sysMemberRai 会员权益
     * @return 会员权益
     */
    @Override
    public List<SysMemberRai> selectSysMemberRaiList(SysMemberRai sysMemberRai)
    {
        return sysMemberRaiMapper.selectSysMemberRaiList(sysMemberRai);
    }

    /**
     * 新增会员权益
     * 
     * @param sysMemberRai 会员权益
     * @return 结果
     */
    @Override
    public int insertSysMemberRai(SysMemberRai sysMemberRai)
    {
//        List<String> raiGoods = sysMemberRai.getRaiGoods();
//        if (ObjectUtils.isNotEmpty(raiGoods)){
//            for (String raiGood : raiGoods){
//                sysMemberRaiMapper.insertRaiGoods(sysMemberRai.getId(),raiGood);
//            }
//        }
        sysMemberRai.setGetStatus("0");
        sysMemberRai.setCreateTime(DateUtils.getNowDate());
        sysMemberRai.setListingTime(DateUtils.getNowDate());
        sysMemberRai.setRaiGoodsParam(sysMemberRai.getRaiGoods().toString());
        sysMemberRai.setEndCollectionTime(getDateTimeAfter30Days());

        return sysMemberRaiMapper.insertSysMemberRai(sysMemberRai);
    }

    /**
     * 修改会员权益
     * 
     * @param sysMemberRai 会员权益
     * @return 结果
     */
    @Override
    public int updateSysMemberRai(SysMemberRai sysMemberRai)
    {
        // 根据会员权益ID查询会员权益
        SysMemberRai param = sysMemberRaiMapper.selectSysMemberRaiById(sysMemberRai.getId());
        // 如果查询到的会员权益不为空且权益商品列表不为空
        if(ObjectUtils.isNotEmpty(param) && ObjectUtils.isNotEmpty(param.getRaiGoods())){
            // 删除旧的权益商品
            sysMemberRaiMapper.deleteRaiGoods(sysMemberRai.getId());
            // 获取新的权益商品列表
            List<String> raiGoods = sysMemberRai.getRaiGoods();
            // 如果新的权益商品列表不为空
            if (ObjectUtils.isNotEmpty(raiGoods)){
                // 遍历新的权益商品列表
                for (String raiGood : raiGoods){
                    // 插入新的权益商品
                    sysMemberRaiMapper.insertRaiGoods(sysMemberRai.getId(),raiGood);
                }
            }
        }
        // 如果上架
        if (ObjectUtils.isNotEmpty(sysMemberRai.getRaiStatus()) && sysMemberRai.getRaiStatus().equals("1")){
            sysMemberRai.setListingTime(DateUtils.getNowDate());
            sysMemberRai.setEndCollectionTime(getDateTimeAfter30Days());
        }else{
            sysMemberRai.setEndCollectionTime(null);
            sysMemberRai.setListingTime(null);
        }
        // 更新会员权益的更新时间
        sysMemberRai.setUpdateTime(DateUtils.getNowDate());

        // 设置会员权益的商品参数
        if (sysMemberRai.getRaiGoods() != null){
            sysMemberRai.setRaiGoodsParam(sysMemberRai.getRaiGoods().toString());
        }
        // 更新会员权益
        return sysMemberRaiMapper.updateSysMemberRai(sysMemberRai);
    }


    /**
     * 批量删除会员权益
     * 
     * @param ids 需要删除的会员权益主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberRaiByIds(String[] ids)
    {
        return sysMemberRaiMapper.deleteSysMemberRaiByIds(ids);
    }

    /**
     * 删除会员权益信息
     * 
     * @param id 会员权益主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberRaiById(String id)
    {
        return sysMemberRaiMapper.deleteSysMemberRaiById(id);
    }

    public static Date getDateTimeAfter30Days() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime after30Days = now.plusDays(30);
        return convertToDate(after30Days);
    }

    /**
     * 将 LocalDateTime 转换为 Date
     * @param localDateTime 要转换的 LocalDateTime
     * @return 对应的 Date 对象
     */
    public static Date convertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

}
